package com.qyhl.webtv.module_user.home;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.entity.intergral.ExchangerRateBean;
import com.qyhl.webtv.commonlib.entity.intergral.IntegralUserInfoBean;
import com.qyhl.webtv.commonlib.entity.user.LuckDrawAdvBean;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_user.app.UserApplication;
import com.qyhl.webtv.module_user.common.UserUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2017/12/5.
 */

public class UserCenterModel implements UserCenterContract.UserCenterModel {
    private UserCenterPresenter mPresenter;
    private final String TAG = "UserCenterTag";


    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    public IntergralService intergralService;

    public UserCenterModel(UserCenterPresenter mPresenter) {
        this.mPresenter = mPresenter;
        ARouter.getInstance().inject(this);
    }

    /**
     * 邀请码提交
     *
     * @param code
     */
    @Override
    public void sendCode(String code) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            String userName = CommonUtils.getInstance().getUserName();

            EasyHttp.get(UserUrl.GET_USER_INVITECODE)
                    .params("username", userName)
                    .params("code", code)
                    .execute(new SimpleCallBack<String>() {
                        @Override
                        public void onError(ApiException e) {
                            mPresenter.sendError("网络异常，提交失败！");
                        }

                        @Override
                        public void onSuccess(String s) {
                            ApiResult apiResult = new Gson().fromJson(s, ApiResult.class);
                            if (apiResult.getCode() == 200) {
                                mPresenter.sendSuccess("提交成功！");
                            } else {
                                mPresenter.sendError(apiResult.getMessage());
                            }
                        }
                    });
        } else {
            mPresenter.sendError("无网络连接，请检查您的网络...");
        }
    }

    /**
     * 请求金币数量和签到信息
     */
    @Override
    public void getIntegralData() {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {

            intergralService.getSignInfo(new IntergralService.SignInCallBack() {
                @Override
                public void success(IntegralUserInfoBean bean) {
                    mPresenter.integralSuccess(bean);
                }

                @Override
                public void error(String msg) {
                    mPresenter.integralError(msg);
                }
            });
        } else {
            mPresenter.integralError("无网络连接，请检查您的网络...");
        }
    }

    /**
     * 获取抽奖广告数据
     */
    @Override
    public void getAdvData() {
        intergralService.getLuckDraw(new IntergralService.LuckDraw() {
            @Override
            public void success(List<LuckDrawAdvBean> listBean) {
                mPresenter.setAdvList(listBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.setAdvList(null);
            }
        });


    }

    /**
     * 请求汇率
     */
    @Override
    public void getExchangerRate() {

        intergralService.getExchangerRate(new IntergralService.ExchangerRate() {
            @Override
            public void success(ExchangerRateBean bean) {
                mPresenter.exchangerRateSuccess(bean);
            }

            @Override
            public void error(String msg) {
                mPresenter.exchangerRateError();
            }
        });

    }

    @Override
    public void getUserInfo(String username) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = UserUrl.GET_USER_INFO_NEW + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&username=" + username))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.userInfoError("网络异常，用户信息更新失败！");
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        mPresenter.userInfoSuccess(userInfoBean);
                    }
                });
    }
}
